package com.cf.moudle.newsfeed;

import com.cf.base.comm.base.BaseApplication;
import com.cf.base.http.HttpManager;
import com.cf.base.http.bean.ErrorResponse;
import com.cf.base.http.converter.fastjson.FastJsonConverterFactory;
import com.cf.base.http.core.RetrofitService;
import com.cf.base.http.engine.impl.FastJsonEngine;
import com.cf.base.http.generator.impl.DefaultBodyErrorAdapter;
import com.cf.base.http.interceptor.headers.HeaderProvider;

import java.util.List;
import java.util.Map;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class NewsFeedApolication extends BaseApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        initHttp();
    }

    void initHttp() {
        HttpManager.getInstance().registerProvider("USER", s ->
                RetrofitService.RetrofitBeanBuilder.create().moduleName("USER")
                        .baseUrl("http://127.0.0,1")
                        .client(new OkHttpClient.Builder())
                        .header(new HeaderProvider() {
                            @Override
                            public Headers defaultHeaders() {
                                return null;
                            }

                            @Override
                            public String[] getHeaderKey() {
                                return new String[0];
                            }

                            @Override
                            public Map<String, String> getHeaderByKey(List<String> list) {
                                return null;
                            }
                        })
                        .retrofitFactory(FastJsonConverterFactory.create(),
                                RxJava2CallAdapterFactory.create())
                        .errorAdapter(
                                new DefaultBodyErrorAdapter<ErrorResponse>(
                                        FastJsonEngine.create()
                                ) {
                                })
                        .build()
        );
    }

}
